package com.deshan.websys.code.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.deshan.websys.code.service.PmsCrawlerService;

import com.ruiyun.jvppeteer.core.Puppeteer;

import com.ruiyun.jvppeteer.core.browser.Browser;
import com.ruiyun.jvppeteer.core.browser.BrowserFetcher;
import com.ruiyun.jvppeteer.core.page.Page;

import com.ruiyun.jvppeteer.options.LaunchOptions;
import com.ruiyun.jvppeteer.options.LaunchOptionsBuilder;
import kong.unirest.JsonNode;
import kong.unirest.Unirest;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.jsoup.nodes.Element;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CompletableFuture;

@Service
public class PmsCrawlerServiceImpl implements PmsCrawlerService {

    public void RedBookCrawler(){
        String url = "https://www.xiaohongshu.com/discovery";
            List<String> imageUrls = new ArrayList<>();
        try {
            Document document = Jsoup.connect(url).get();
            Elements elements = document.select("img.img-responsive.lazy"); // 选择包含图片的元素
            for (Element element : elements) {
                String imageUrl = element.attr("data-original"); // 获取图片链接
                imageUrls.add(imageUrl);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
//        return imageUrls;
    }
    public void xiguanCrawler(String keyword){
        String url="https://www.ixigua.com/home/3276166340814919/hotsoon/";

        List<String> imageUrls = new ArrayList<>();
        try {
            Document document = Jsoup.connect(url).get();
            Elements svg=  document.getElementsByTag("body");

            Elements elements1=  document.select("div[class=HorizontalFeedCard searchPageV2__card]");
            Elements elements = document.getElementsByClass("HorizontalFeedCard searchPageV2__card"); // 选择包含图片的元素
            for (Element element : elements) {
                String imageUrl = element.attr("data-original"); // 获取图片链接
                imageUrls.add(imageUrl);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Override
    public  List<HashMap> getBaidu(String keyword)  {
        List<HashMap> listHashMap=new ArrayList<>();
        try {
//            RevisionInfo revisionInfo2 = Puppeteer.downloadBrowser("128.0.6613.137");
//            System.out.println("revisionInfo2: " + revisionInfo2);

            String path = new String("D:\\workspace\\shande-backend-code\\nacos\\plugins\\.local-browser\\win64-722234\\chrome-win\\chrome.exe".getBytes(), "UTF-8");
//            String path = new String("D:\\IDEAWork\\web-service\\.local-browser\\win64-722234\\chrome-win\\chrome.exe".getBytes(), "UTF-8");
            ArrayList<String> argList = new ArrayList<>();
            LaunchOptions options = new LaunchOptionsBuilder().withArgs(argList).withHeadless(false).withExecutablePath(path).build();
            argList.add("--no-sandbox");
            argList.add("--disable-setuid-sandbox");
            //或者
//            ArrayList<String> argList = new ArrayList<>();
//            //自动下载，第一次下载后不会再下载
////            BrowserFetcher.downloadIfNotExist(null);
//            LaunchOptions options = new LaunchOptionsBuilder().withArgs(argList).withHeadless(false).build();
//            argList.add("--no-sandbox");
//            argList.add("--disable-setuid-sandbox");
            Browser browser =  Puppeteer.launch(options);
            Page page = browser.newPage();
            page.goTo("https://www.ixigua.com/search/"+keyword+"/?logTag=423ac644324e5c15d0b4&tab_name=home&fss=input");

            CompletableFuture<Void> future=CompletableFuture.runAsync(()->{
                try {
                    Thread.sleep(5000);
                    String content = page.content();
                    browser.close();
                    Document document = Jsoup.parse(content);
                    // 找出我们上面说的那个class所在的div标签
//                    Elements elements = document.getElementsByClass("FeedContainer__itemWrapper");
                    Elements elements = document.getElementsByClass("HorizontalFeedCard searchPageV2__card");

                    // 去 class所在的div标签中找出需要的 字段信息
                    for (int i = 0; i < elements.size(); i++) {
                        HashMap resultMap=new HashMap();


                        Element element = elements.get(i);
                        org.jsoup.select.Elements imgelemet=  element.getElementsByClass("tt-img BU-MagicImage");
                        String imgurl= imgelemet.attr("src");
                        resultMap.put("imgurl",imgurl);

                        System.out.println(imgurl);
                        org.jsoup.select.Elements contentlemet=  element.getElementsByClass("HorizontalFeedCard__title color-link-content-primary");
                        String title= contentlemet.attr("title");
                        resultMap.put("title",title);

                        String href= contentlemet.attr("href");
                        resultMap.put("url",href);

                        System.out.println(title);
                        listHashMap.add(resultMap);
                    }
                }catch (InterruptedException e){

                }
            });

        }catch (Exception ex) {
            //指定启动路径，启动浏览器
            ex.printStackTrace();

        }
        try {
            Thread.sleep(10000);
        }catch (Exception ex){

        }
        return listHashMap;

    }
}
